import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/password',
    name: 'password',
    component: () => import('@/views/login/Passwords.vue')
  },
  {
    path: '/personalInfo',
    name: 'personalInfo',
    component: () => import('@/views/reviewsManage/personalInfo.vue')
  },
  {
    path: '/',
    component: Home,
    children:[
        {
            path:'/',
            name:"basicInformation",
            // component:() => import('@/views/informationEntry/BasicInformation.vue')
            component:() => import('@/views/informationEntry/BasicInfomationEntry.vue')
        },
      {
          path:'/overview',
          name:"overview",
          component:() => import('@/views/home/Overview.vue')
      },
      {
          path:'/settings',
          name:"settings",
          component:() => import('@/views/settings/Settings.vue')
      },
        {
            path:'/informationEntry',
            name:"informationEntry",
            component:() => import('@/views/informationEntry/InformationEntry.vue')
        },
      {
          path:'/applyForTitle',
          name:"applyForTitle",
          component:() => import('@/views/informationEntry/applyForTitle.vue')
      },
      {
          path:'/dataEntry',
          name:"dataEntry",
          component:() => import('@/views/informationEntry/DataEntry.vue')
      },
      {
          path:'/personalHomepage',
          name:"personalHomepage",
          component:() => import('@/views/home/PersonalHomepage.vue')
      },
      {
          path:'/informationQuery',
          name:"informationQuery",
          component:() => import('@/views/informationQuery/InformationQuery.vue')
      },
      {
        path:'/pendingReviews',
        name:"pendingReviews",
        component:() => import('@/views/reviews/pendingReviews.vue')
      },
      {
        path:'/reviewed',
        name:"reviewed",
        component:() => import('@/views/reviews/reviewed.vue')
      },
      {
        path:'/reviewsInfo',
        name:"reviewsInfo",
        component:() => import('@/views/reviews/reviewsInfo.vue')
      },
      {
        path:'/staffTitles',
        name:"staffTitles",
        component:() => import('@/views/staffTitles/staffTitles.vue')
      },
      {
        path:'/dataQuery',
        name:"dataQuery",
        component:() => import('@/views/dataQuery/dataQuery.vue')
      },
      {
        path:'/permissionSettings',
        name:"permissionSettings",
        component:() => import('@/views/permission/PermissionSettings.vue')
      },
      {
        path:'/personnelInfo',
        name:"personnelInfo",
        component:() => import('@/views/informationQuery/personnelInfo.vue')
      },
      {
        path:'/infoDetail',
        name:"infoDetail",
        component:() => import('@/views/informationQuery/infoDetail.vue')
      },
      {
        path:'/updateInfo',
        name:"updateInfo",
        component:() => import('@/views/informationQuery/updateInfo.vue')
      },
      {
        path:'/participationManagement',
        name:"participationManagement",
        component:() => import('@/views/participationManagement/Index.vue')
      },
      {
        path:'/ListOfMatchingPeople',
        name:"ListOfMatchingPeople",
        component:() => import('@/views/participationManagement/ListOfMatchingPeople.vue'),
      },
      {
        path:'/ListOfNonconformingPersons',
        name:"ListOfNonconformingPersons",
        component:() => import('@/views/participationManagement/ListOfNonconformingPersons.vue'),
      },
      {
        path:'/reviewsApply',
        name:"reviewsApply",
        component:() => import('@/views/reviewsManage/reviewsApply.vue'),
      },
      {
        path:'/reviewsPersonnel',
        name:"reviewsPersonnel",
        component:() => import('@/views/reviewsManage/reviewsPersonnel.vue'),
      },
      {
        path:'/expertManage',
        name:"expertManage",
        component:() => import('@/views/reviewsManage/expertManage.vue'),
      },
      {
        path:'/reviewSetting',
        name:"reviewSetting",
        component:() => import('@/views/reviewsManage/reviewSetting.vue'),
      },
      {
        path:'/expertNotice',
        name:"expertNotice",
        component:() => import('@/views/reviewsManage/expertNotice.vue'),
      },
      {
        path:'/expertReviews',
        name:"expertReviews",
        component:() => import('@/views/reviewsManage/expertReviews.vue'),
      },
      {
        path:'/reviewResults',
        name:"reviewResults",
        component:() => import('@/views/reviewsManage/reviewResults.vue'),
      },
      {
        path:'/reviewResultsAgain',
        name:"reviewResultsAgain",
        component:() => import('@/views/reviewsManage/reviewResultsAgain.vue'),
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  from;

  var noRequiredLoginPage = ["login","register","password"];
  if(localStorage.getItem("REVIEW-TOKEN") == null){
      // token失效时，只能跳转至登录、注册等页面
      if(noRequiredLoginPage.indexOf(to.name)  != -1){
          next();
      }else{
          next({ name: 'login'});
      }
  }else{
      // token有效时，不能跳转至登录、注册等页面
      if(noRequiredLoginPage.indexOf(to.name)  != -1){
        next('/basicInformation');
      }else{
        next();
      }
  }
});


// 捕获路由异常
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
